package com.controller;

import com.pojo.Goods;
import com.pojo.Result;
import com.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lifeng
 * @create 2022-11-18 11:32
 */
@RestController
//@RequestMapping("goods")
@CrossOrigin//解决跨域
public class GoodsController {

    //注入service层s
    @Autowired
    private GoodsService goodsService;

    @GetMapping()
    public Result findAll(){
        Result result = new Result();
        List<Goods> list = goodsService.findAll();
        if (list != null){
            //查询成功
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }else {
            //查询失败
            result.setCode(500);
            result.setMsg("查询失败");
        }

        return result;

    }

    /**
     * 测试添加功能
     */
    @PostMapping
    public Result add(){
        Result result = new Result();
        boolean b = goodsService.insertGoods();
        if (b){
            //添加成功
            result.setCode(200);
            result.setMsg("添加成功");
        }else {
            //添加失败
            result.setCode(500);
            result.setMsg("添加失败");
        }

        return result;

    }


    @GetMapping("/selectById/{id}")
    public void selectById(@PathVariable("id") Integer id){
        Goods goods = goodsService.findById(id);
        System.out.println(goods);
    }

}
